layui.use(['table', 'admin', 'ax', 'ztree'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztree;

    /**
     * 系统管理--部门管理
     */
    var Dept = {
        tableId: "dataTable",
        condition: {
            deptId: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Dept.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'deptName', sort: false, title: '部门名称'},
            {field: 'createTime', sort: true, title: '创建时间'},

            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Dept.search = function () {
        var queryData = {};
        queryData['deptName'] = $("#deptName").val();
        queryData['deptId'] = Dept.condition.deptId;
        table.reload(Dept.tableId, {where: queryData});
    };


    Dept.onClickDept = function (e, treeId, treeNode) {
        Dept.condition.deptId = treeNode.id;
        Dept.search();
    };


   Dept.openAddDept = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '新增部门',
            area: ['600px','500px'],
            content: Sam.ctxPath + '/sam/dept/add?pid='+Dept.condition.deptId,
            end: function () {
                admin.getTempData('formOk') && Dept.search();
            }
        });
    };

    //  Dept.onAddSubDept = function (data) {
    //     admin.putTempData('formOk', false);
    //     top.layui.admin.open({
    //         type: 2,
    //         title: '添加子级部门',
    //         content: Sam.ctxPath + '/sam/dept/add?op=sub&id=' + data.id,
    //         end: function () {
    //             admin.getTempData('formOk') && Dept.search();
    //         }
    //     });
    // };

    Dept.onAddUser = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '新增用户',
            area: ['750px','700px'],
            content: Sam.ctxPath + '/sam/user/add?deptId=' + data.id,
            end: function () {
                admin.getTempData('formOk') && Dept.search();
            }
        });
    };


    Dept.onEditDept = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑部门',
            area: ['600px','500px'],
            content: Sam.ctxPath + '/sam/dept/edit/' + data.id,
            end: function () {
                admin.getTempData('formOk') && Dept.search();
            }
        });
    };


    Dept.onDeleteDept = function (data) {
        var operation = function () {
            var ajax = new $ax(Sam.ctxPath + "/sam/dept/delete/"+data.id, function () {
                Sam.success("删除成功!");
                Dept.search();
            }, function (data) {
                Sam.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.start();
        };
        Sam.confirm("是否确认删除该部门以及部门所有成员吗?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Dept.tableId,
        url: Sam.ctxPath + '/sam/dept/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Dept.initColumn()
    });

    //初始化左侧部门树
    var ztree = new $ZTree("deptTree", "/sam/dept/tree");
    ztree.bindOnClick(Dept.onClickDept);
    ztree.init();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Dept.search();
    });

    $('#btnAdd').click(function () {
        Dept.openAddDept();
    });


    // 工具条点击事件
    table.on('tool(' + Dept.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Dept.onEditDept(data);
        } else if (layEvent === 'delete') {
            Dept.onDeleteDept(data);
        }
        // else if (layEvent === 'add') {
        //     Dept.onAddDept(data);
        // }
        // else if (layEvent === 'addSub') {
        //     Dept.onAddSubDept(data);
        // }
        else if (layEvent === 'addUser') {
            Dept.onAddUser(data);
        }
    });
});
